*** Figures

clear all
set more off
set maxvar 20000

** note: type your directory here
global dir_dest "/replication/Data/"
global dir_dest_figures "/replication/Figures/"
global dir_dest_tables "/replication/Tables/" 
	   
***********************************************************************************************************
// Figure 3. The number of migrant-related regulations and regulation scores (1995–2007), total and by topic
***********************************************************************************************************

set more off
use ${dir_dest}1_regulation_coding.dta,clear
keep if year>=1995 & year<=2007
keep if ct==1
gen n=1
encode cat,gen(cat1)
drop cat
rename cat1 cat
collapse (sum) n scorehc,by(citygb provgb year cat)
reshape wide scorehc n, i(citygb provgb year) j(cat)
recode n* (.=0)
recode scorehc* (.=0)
gen n=n1+n2+n3+n4+n5+n6
gen score=scorehc1+scorehc2+scorehc3+scorehc4+scorehc5+scorehc6
rename scorehc1 score_birth
rename scorehc2 score_hukou
capture rename scorehc3 score_welfare
rename scorehc4 score_other
rename scorehc5 score_insurance
rename scorehc6 score_work

collapse (sum) n score score_birth score_hukou score_insurance score_other score_work score_welfare,by(year)
foreach var of varlist n score score_birth score_hukou score_insurance score_other score_work score_welfare {
replace `var'=`var'/340
}
label var year "year"

// Panel (a)
twoway (connected n year, yaxis(1) lpattern(dash) msymbol(circle_hollow) mcolor(red) lcolor(red)) ///
       (connected score year, yaxis(2) lpattern(solid) msymbol(square) mcolor(navy) lcolor(navy)) ///
     ,graphregion(color(white)) legend(order(1 "# of regulation" 2 "Regulation score")) ///
      xlabel(1995 2001 2007) xline(2001) ytitle("Average # of regulations", axis(1)) ///
	  ytitle("Average regulation scores", axis(2))
graph export ${dir_dest_figures}1_reg_score_trend.png,replace

// Panel (b)
twoway (connected score_hukou year, lpattern(shortdash) msymbol(diamond) )  /// 
       (connected score_insurance year, lpattern(dash) msymbol(square) )  /// 
       (connected score_work year, lpattern(solid) msymbol(circle) )  /// 
       (connected score_welfare year, lpattern(dot) msymbol(triangle) )  /// 
       ,graphregion(color(white)) xlabel(1995 2001 2007) xline(2001) ///
       legend(size(small) col(4) order(1 "Hukou" 2 "Social insurance" 3 "Work" 4 "Local public goods" )) ///
	   ytitle("Average regulation scores") 
graph export ${dir_dest_figures}2_reg_bytype_trend.png,replace

***********************************************************************************************************
// Figure 4. The number of regulations in the fiscal-related and resource-related categories (1995–2007)
***********************************************************************************************************

use ${dir_dest}5_placebo_regulation.dta,clear

twoway (connected fisc year, yaxis(1) lpattern(dash) msymbol(circle_hollow) mcolor(red) lcolor(red)) ///
       (connected resource year, yaxis(2) lpattern(solid) msymbol(square) mcolor(navy) lcolor(navy)) ///
     ,graphregion(color(white)) legend(order(1 "# of fiscal regulations" 2 "# of resource regulations")) ///
      xlabel(1995 2001 2007) xline(2001) ytitle("Fiscal, per prefecture",axis(1)	)  ///
	  ytitle("Resource, per prefecture",axis(2))
	  
graph export ${dir_dest_figures}trend_fiscal_resource.png,replace	 


***********************************************************************************************************
// Figure 5. The trends of tariffs on exports and export values (1995–2007) and export supply elasticities (1995–2001 and 2001–2007)
***********************************************************************************************************

// Panel (a)
use ${dir_dest}6_trade_trend.dta,clear

twoway (connected wt_ahs_1995 year, yaxis(1) xlabel(1995 2001 2007) msymbol(square_hollow) mcolor(red) lcolor(red) lpattern(dash)) ///
       (connected imp year, yaxis(2) msymbol(circle) mcolor(navy) lcolor(navy)), ///
 legend(order(1 "Tariffs on exports" 2 "Export value" ) ) ///
 graphregion(color(white)) ytitle("Weighted average tariff, percentage point", axis(1)) ///
 ytitle("Value of exports,1 billion USD", axis(2)) ///
 xtitle("Year") xline(2001)
graph export  ${dir_dest_figures}2_tariff_trade_trend.png,replace


// Panel (b)     
use ${dir_dest}7_trade_elasticity.dta,clear

twoway (scatter d_l_imp1995 d_t1995, msymbol(triangle_hollow) mcolor(red) mlabcolor(red) xlabel(-4 -3 -2 -1 0 1)  text( .934279 -4.656289  "Slope of both lines ~ –0.13", color(navy) place(se) size(small)) ) ///
       (lfit d_l_imp1995 d_t1995, lcolor(gs1)) ///
       (scatter d_l_imp2001 d_t2001, msymbol(square) mcolor(navy) mlabcolor(navy)) ///
	   (lfit d_l_imp2001 d_t2001, lcolor(gs5)) ///
	   ,legend(order (1 "1995–2001"  3 "2001–2007" )) ///
	   graphregion(color(white)) xtitle("Percentage point change in tariff") ///
	   ytitle("Change in log exports")
graph export ${dir_dest_figures}3_trade_elasticity_both.png,replace	   
 

***********************************************************************************************************
// Figure 6. The correlation between the changes in tariffs on exports (2001–2007) with the changes in exports values (1995–2001), and with the changes in tariffs on exports (1995–2001)
***********************************************************************************************************
use ${dir_dest}7_trade_elasticity.dta,clear

twoway (scatter d_t2001 d_t1995,mlabel(SIC) mlabcolor(navy) msymbol(x) mcolor(navy) ) ///
       (lfit d_t2001 d_t1995,mlabel(SIC) lcolor(navy)), ///
    graphregion(color(white))  xtitle("Percentage-point change in tariff, 1995—2001") ///
	ytitle("Percentage-point change in tariff, 2001—2007") legend(off)	///
	text( .25 -4.65  "Slope = –0.03 (s.e. = 0.1)", color(red) place(se) size(small)) 
graph export ${dir_dest_figures}5_tariff_pre_post.png,replace

twoway (scatter d_t2001 d_l_imp1995,mlabel(SIC) mlabcolor(navy) msymbol(x) mcolor(navy)) ///
       (lfit  d_t2001 d_l_imp1995, lcolor(navy)), ///
    graphregion(color(white))  xtitle("Change in log exports, 1995—2001") ///
	ytitle("Percentage-point change in tariff, 2001—2007") legend(off) ///	 
	text( .5 1.2  "Slope = 0.03 (s.e. = 0.3)", color(red) place(se) size(small)) 	
graph export ${dir_dest_figures}6_tariff_post_exp_post.png,replace	

***********************************************************************************************************
// Figure 7. Prefecture-level trends in wages and per capita GDP (1995–2007), by size of declines in tariffs on exports (2001–2007)
***********************************************************************************************************
clear all
set more off 
use ${dir_dest}8_wage_gdp_trend.dta,clear

// Panel (a)
twoway (connected wage year if pct==1,xline(2001) msymbol(x) mcolor(eltblue) lcolor(eltblue) lpattern(shortdash)) ///
(connected wage year if pct==2, msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
(connected wage year if pct==3,msymbol(square) mcolor(navy) lcolor(navy) ) ///
   ,legend(order(1 "Small decline" 2 "Medium decline" 3 "Big decline")) graphregion(color(white)) ///
    ytitle("Annual wage (Unit: 1,000 yuan)") xlabel(1995(2)2007)
graph export ${dir_dest_figures}/wage.png,replace  

// Panel (b)
twoway (connected gdppct year if pct==1,xline(2001) msymbol(x) mcolor(eltblue) lcolor(eltblue) lpattern(shortdash)) ///
(connected gdppct year if pct==2, msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
(connected gdppct year if pct==3,msymbol(square) mcolor(navy) lcolor(navy) ) ///
   ,legend(order(1 "Small decline" 2 "Medium decline" 3 "Big decline")) graphregion(color(white)) ///
   ytitle("Per capita GDP (Unit: 1,000 yuan)")  xlabel(1995(2)2007)
graph export ${dir_dest_figures}/gdppct.png,replace   

***********************************************************************************************************
// Figure 8. The relationship between prefecture-level changes in tariffs on exports and regulation changes (2001–2007)
***********************************************************************************************************
use ${dir_dest}2_main_regression.dta,clear
gen cname1="Beijing" if citygb==1101
gen cname2="Shanghai" if citygb==3101
gen cname3="Guangzhou" if citygb==4401

keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001

reg d_lhc tf_exp_wage if year==2001,cluster(provgb)
reg d_lhc tf_exp_wage if year==2001 [aw=totpop_st],cluster(provgb)

twoway (scatter d_lhc tf_exp_wage if year==2001,  msymbol(smdiamond) mcolor(navy) text(0.8 -0.6 "Slope = –0.90 (s.e. = 0.41)", color(navy)  place(se) size(small)) text(2.4 -0.5 "Slope = –1.84 (s.e. = 0.57)", color(red) place(s) size(small))) ///
       (scatter d_lhc tf_exp_wage if year==2001 & cname1!="",mlabel(cname1) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(6) ) ///
	   (scatter d_lhc tf_exp_wage if year==2001 & cname2!="",mlabel(cname2) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(12) ) ///
	   (scatter d_lhc tf_exp_wage if year==2001 & cname3!="",mlabel(cname3) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(2) ) ///
       (lfit d_lhc tf_exp_wage if year==2001  , lcolor(navy) lpattern(dash))  ///
	   (lfit d_lhc tf_exp_wage if year==2001 [aw=totpop_st] , lcolor(red) lpattern(shortdash_dot))  ///
	   ,graphregion(color(white)) xtitle("Changes in tariffs on exports, 2001—2007") ///
	   ytitle("Changes in sinh{sup:-1} (regulation score), 2001—2007")  ///
	   legend(order(5 "Unweighted" 6 "Weighted by 2001 population"))	   
graph export ${dir_dest_figures}/tariff_regulation.png,replace 

***********************************************************************************************************
// Figure 9. Average prefecture-level regulation changes (1995–2007), by the size of the change in tariffs on exports (2001–2007) and by migrant intensity (2001)
***********************************************************************************************************

// Panel (a)
clear all
set more off    
use ${dir_dest}2_main_regression.dta,clear
keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001
sum tf_exp_wage,detail
xtile pct=-tf_exp_wage, n(2)
xtile pct1=m_intensity,n(2)
gen type=1 if pct==1 & pct1==1
replace type=2 if pct==1 & pct1==2
replace type=3 if pct==2 & pct1==1
replace type=4 if pct==2 & pct1==2
keep citygb type
bysort type: gen n_city=_N
save ${dir_dest}big.dta,replace

set more off
use ${dir_dest}1_regulation_coding.dta,clear
gen n=1
merge m:1 citygb using ${dir_dest}big.dta
drop if _merge!=3
drop _merge
drop if type==.
collapse (sum) scorehc (mean) n_city, by(year type)
rename scorehc score
replace score = score/n_city
drop n_city
keep if year>=1995 & year<=2007
sort year
reshape wide score, i(year) j(type)		
recode * (.=0)
label var year "year"
label var score1 "average regulation score, small decline and local intensive"
label var score2 "average regulation score, small decline and migrant intensive"
label var score3 "average regulation score, big decline and local intensive"
label var score4 "average regulation score, big decline and migrant intensive"
twoway (connected score1 year,msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
       (connected score2 year ,msymbol(circle) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
	   (connected score3 year,msymbol(square_hollow) mcolor(navy) lcolor(navy) ) ///
	   (connected score4 year,msymbol(square) mcolor(navy) lcolor(navy) ) ///
       ,graphregion(color(white)) ytitle("Score of new regulations, group average") ///
	   legend(order(1 "Small decline, local" 2 "Small decline, migrant" 3 "Big decline, local" 4 "Big decline, migrant")) ///
	   xline(2001) xlabel(1995(2)2007) graphregion(color(white))  ///
	   title("(a) By size of decline in tariffs and migrant intensity")
graph export ${dir_dest_figures}/big_small_local_migrant.png,replace	

// Panel (b)
clear all
set more off
use ${dir_dest}2_main_regression.dta,clear
keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001
sum tf_exp_wage,detail
xtile pct=-tf_exp_wage, n(2)
xtile pct1=pvt_share,n(2)
gen type=1 if pct==1 & pct1==1
replace type=2 if pct==1 & pct1==2
replace type=3 if pct==2 & pct1==1
replace type=4 if pct==2 & pct1==2
keep citygb type
bysort type: gen n_city=_N
save ${dir_JMP_v3}big.dta,replace

set more off
use ${dir_dest}1_regulation_coding.dta,clear
gen n=1
merge m:1 citygb using ${dir_dest}big.dta
drop if _merge!=3
drop _merge
drop if type==.
collapse (sum) scorehc (mean) n_city, by(year type)
rename scorehc score
replace score = score/n_city
drop n_city
keep if year>=1995 & year<=2007
sort year
reshape wide score, i(year) j(type)		
recode * (.=0)
label var year "year"
label var score1 "average regulation score, small decline and non-private"
label var score2 "average regulation score, small decline and private"
label var score3 "average regulation score, big decline and non-private"
label var score4 "average regulation score, big decline and private"

twoway (connected score1 year,msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
       (connected score2 year ,msymbol(circle) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
	   (connected score3 year,msymbol(square_hollow) mcolor(navy) lcolor(navy) ) ///
	   (connected score4 year,msymbol(square) mcolor(navy) lcolor(navy) ) ///
       ,graphregion(color(white)) ytitle("Score of new regulations, group average") ///
	   legend(order(1 "Small decline, non-private" 2 "Small decline, private" 3 "Big decline, non-private" 4 "Big decline, private")) ///
	   xline(2001) xlabel(1995(2)2007) graphregion(color(white)) ///
	   title("(b) By size of decline in tariffs and private sales share")
graph export ${dir_dest_figures}/big_small_soe_pvt.png,replace

// Panel (c)   
clear all
set more off
use ${dir_dest}2_main_regression.dta,clear
keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001
sum tf_exp_wage,detail
xtile pct=-tf_exp_wage,n(2)
xtile pct1=l_gdppct_st,n(2)
gen type=1 if pct==1 & pct1==1
replace type=2 if pct==1 & pct1==2
replace type=3 if pct==2 & pct1==1
replace type=4 if pct==2 & pct1==2
keep citygb type
bysort type: gen n_city=_N
save ${dir_dest}big.dta,replace

set more off
use ${dir_dest}1_regulation_coding.dta,clear
gen n=1
merge m:1 citygb using ${dir_dest}big.dta
drop if _merge!=3
drop _merge
drop if type==.
collapse (sum) scorehc (mean) n_city, by(year type)
rename scorehc score
replace score = score/n_city
drop n_city
keep if year>=1995 & year<=2007
sort year
reshape wide score, i(year) j(type)	
recode * (.=0)
label var year "year"
label var score1 "average regulation score, small decline and poor"
label var score2 "average regulation score, small decline and rich"
label var score3 "average regulation score, big decline and poor"
label var score4 "average regulation score, big decline and rich"
twoway (connected score1 year,msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
       (connected score2 year ,msymbol(circle) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
	   (connected score3 year,msymbol(square_hollow) mcolor(navy) lcolor(navy) ) ///
	   (connected score4 year,msymbol(square) mcolor(navy) lcolor(navy) ) ///
       ,graphregion(color(white)) ytitle("Score of new regulations, group average") ///
	   legend(order(1 "Small decline, poor" 2 "Small decline, rich" 3 "Big decline, poor" 4 "Big decline, rich")) ///
	   xline(2001) xlabel(1995(2)2007) graphregion(color(white)) ///
	   title("(c) By size of decline in tariffs and city income")
graph export ${dir_dest_figures}/big_small_rich_poor.png,replace


// Panel (d)   
clear all
set more off
use ${dir_dest}2_main_regression.dta,clear
keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001
sum tf_exp_wage,detail
xtile pct=-tf_exp_wage, n(2)
xtile pct1=l_wage_st,n(2)
gen type=1 if pct==1 & pct1==1
replace type=2 if pct==1 & pct1==2
replace type=3 if pct==2 & pct1==1
replace type=4 if pct==2 & pct1==2
keep citygb type
bysort type: gen n_city=_N
save ${dir_dest}big.dta,replace

set more off
use ${dir_dest}1_regulation_coding.dta,clear
gen n=1
merge m:1 citygb using ${dir_dest}big.dta
drop if _merge!=3
drop _merge
drop if type==.
collapse (sum) scorehc (mean) n_city, by(year type)
rename scorehc score
replace score = score/n_city
drop n_city
keep if year>=1995 & year<=2007
sort year
reshape wide score, i(year) j(type)	
recode * (.=0)
label var year "year"
label var score1 "average regulation score, small decline and low wage"
label var score2 "average regulation score, small decline and high wage"
label var score3 "average regulation score, big decline and low wage"
label var score4 "average regulation score, big decline and high wage"
twoway (connected score1 year,msymbol(circle_hollow) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
       (connected score2 year ,msymbol(circle) mcolor(emidblue) lcolor(emidblue) lpattern(dash)) ///
	   (connected score3 year,msymbol(square_hollow) mcolor(navy) lcolor(navy) ) ///
	   (connected score4 year,msymbol(square) mcolor(navy) lcolor(navy) ) ///
       ,graphregion(color(white)) ytitle("Score of new regulations, group average") ///
	   legend(order(1 "Small decline, low wage" 2 "Small decline, high wage" 3 "Big decline, low wage" 4 "Big decline, high wage")) ///
	   xline(2001) xlabel(1995(2)2007) graphregion(color(white)) ///
	   title("(d) By size of decline in tariffs and city wage level")
graph export ${dir_dest_figures}/big_small_high_low.png,replace


***********************************************************************************************************
// Figure 10. The relationship between changes in tariffs on exports (2001-2007), changes in the regulation score (2001-2007), and changes in the migrant share of population (2000-2010)
***********************************************************************************************************

use ${dir_dest}2_main_regression.dta,clear
keep if d_l_gdppct!=.
keep if lambda13<=.0548622
keep if year==2001
gen cname1="Beijing" if citygb==1101
gen cname2="Shanghai" if citygb==3101
gen cname3="Guangzhou" if citygb==4401

// panel (a)
reg d_in_pct tf_exp_wage if year==2001,cluster(provgb)
reg d_in_pct tf_exp_wage  if year==2001  [aw=totpop_st],cluster(provgb)
	   
	   	twoway (scatter d_in_pct tf_exp_wage  if year==2001,  msymbol(smdiamond) mcolor(navy) text(14.5 0 "Slope = –5.77 (s.e = 2.55)", color(navy)  place(se) size(small)) text(-1 0.1 "Slope = –11.99 (s.e. = 4.21)", color(red) place(s) size(small))) ///
       (scatter d_in_pct tf_exp_wage if year==2001 & cname1!="",mlabel(cname1) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(7) ) ///
	   (scatter d_in_pct tf_exp_wage if year==2001 & cname2!="",mlabel(cname2) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(11) ) ///
	   (scatter d_in_pct tf_exp_wage if year==2001 & cname3!="",mlabel(cname3) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(1) ) ///
       (lfit d_in_pct tf_exp_wage if year==2001 , lcolor(navy) lpattern(dash))  ///
	   (lfit d_in_pct tf_exp_wage if year==2001 [aw=totpop_st] , lcolor(red) lpattern(shortdash_dot))  ///
	   ,graphregion(color(white)) xtitle("Changes in tariffs on exports, 2001—2007") ///
	   ytitle("Change in migrant share of pop. (%), 2000—2010")  ///
	   legend(order(5 "Unweighted" 6 "Weighted by 2001 population"))
graph export ${dir_dest_figures}/tradeshock_migrant.png,replace 

// panel (b)
reg d_in_pct d_lhc if year==2001,cluster(provgb)
reg d_in_pct d_lhc  if year==2001  [aw=totpop_st],cluster(provgb)
	   
	   twoway (scatter d_in_pct d_lhc  if year==2001,  msymbol(smdiamond) mcolor(navy) text(5 3 "Slope = 1.47 (s.e. = 0.33)", color(navy)  place(se) size(small)) text(17 3.8 "Slope = 2.13 (s.e. = 0.32)", color(red) place(s) size(small))) ///
       (scatter d_in_pct d_lhc if year==2001 & cname1!="",mlabel(cname1) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(3) ) ///
	   (scatter d_in_pct d_lhc if year==2001 & cname2!="",mlabel(cname2) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(11) ) ///
	   (scatter d_in_pct d_lhc if year==2001 & cname3!="",mlabel(cname3) mlabcolor(emidblue) msymbol(circle) mcolor(emidblue) mlabp(3) ) ///
       (lfit d_in_pct d_lhc if year==2001 , lcolor(navy) lpattern(dash))  ///
	   (lfit d_in_pct d_lhc if year==2001 [aw=totpop_st] , lcolor(red) lpattern(shortdash_dot))  ///
	   ,graphregion(color(white)) xtitle("Changes in sinh{sup:-1} (regulation score), 2001—2007") ///
	   ytitle("Changes in migrant share of pop. (%), 2000—2010")  ///
	   legend(order(5 "Unweighted" 6 "Weighted by 2001 population"))	   
graph export ${dir_dest_figures}/regulation_migrant.png,replace 
